package org.itzixi.controller;


import cn.hutool.core.util.StrUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.itzixi.RequestConstant;
import org.itzixi.enums.YesOrNo;
import org.itzixi.grace.result.GraceJSONResult;
import org.itzixi.grace.result.ResponseStatusEnum;
import org.itzixi.pojo.Friendship;
import org.itzixi.pojo.vo.ContactsVO;
import org.itzixi.service.FriendshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/friendship")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FriendshipController {

    private final FriendshipService friendshipService;

    @PostMapping("/getFriendship")
    public GraceJSONResult getFriendship(String friendId, HttpServletRequest request){
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        Friendship friendship = friendshipService.getFriendship(myId, friendId );
        return GraceJSONResult.ok(friendship);
    }


    @PostMapping("/queryMyFriends")
    public GraceJSONResult queryMyFriends(HttpServletRequest request){
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StrUtil.isBlank(myId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        }
        List<ContactsVO> contactsVOList = friendshipService.queryMyFriends(myId, false);
        return GraceJSONResult.ok(contactsVOList);
    }

    @PostMapping("/updateFriendRemark")
    public GraceJSONResult updateFriendRemark(HttpServletRequest request, String friendId, String friendRemark){
        if (StringUtils.isBlank(friendId) || StringUtils.isBlank(friendRemark))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StrUtil.isBlank(myId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        }

        friendshipService.updateFriendRemark(myId, friendId, friendRemark);
        return GraceJSONResult.ok();


    }

    @PostMapping("/tobeBlack")
    public GraceJSONResult tobeBlack(String friendId, HttpServletRequest request){
        if (StringUtils.isBlank(friendId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(myId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);

        friendshipService.updateBlackList(myId, friendId, YesOrNo.YES);



        return GraceJSONResult.ok();
    }

    @PostMapping("/moveOutBlack")
    public GraceJSONResult moveOutBlack( String friendId, HttpServletRequest request){
        if (StringUtils.isBlank(friendId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(myId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        friendshipService.updateBlackList(myId, friendId, YesOrNo.NO);
        return GraceJSONResult.ok();
    }


    @PostMapping("/queryMyBlackList")
    public GraceJSONResult queryMyBlackList( HttpServletRequest request){
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StrUtil.isBlank(myId)){
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        }
        List<ContactsVO> contactsVOList = friendshipService.queryMyFriends(myId, true);
        return GraceJSONResult.ok(contactsVOList);
    }

    @PostMapping("/delete")
    public GraceJSONResult delete( HttpServletRequest request,String friendId){
        if (StringUtils.isBlank(friendId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);
        String myId = request.getHeader(RequestConstant.HEADER_USER_ID);
        if (StringUtils.isBlank(myId))
            return GraceJSONResult.errorCustom(ResponseStatusEnum.USER_PARAMS_ERROR);

        friendshipService.delete(myId, friendId);
        return GraceJSONResult.ok();
    }

    @GetMapping("/isBlack")
    public GraceJSONResult isBlack(String friendId1st, String friendId2nd){
        // 需要进行两次查询，是否互相拉黑
        boolean friendlyFlag = friendshipService.isBlackEachOther(friendId1st, friendId2nd);
        return GraceJSONResult.ok(friendlyFlag);
    }
}
